package edu.bupt.c2string;

import java.util.LinkedList;

public class T15SimplifyPath {
    public String simplifyPath(String path) {
        String[] split = path.split("/");
        LinkedList<String> strings = new LinkedList<String>();
        for (int i=1;i<split.length;i++){
            if ("".equals(split[i]) || ".".equals(split[i])){
                continue;
            } else if("..".equals(split[i])){
                if (strings.size()>0)
                    strings.removeLast();
            } else {
                strings.add(split[i]);
            }
        }
        StringBuilder stringBuilder = new StringBuilder("");
        for (String i:strings){
            stringBuilder.append("/");
            stringBuilder.append(i);
        }
        if(stringBuilder.length() == 0) stringBuilder.append("/");
        return stringBuilder.toString();
    }
}
